package lucene.service.impl;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lucene.dao.IndexingGoodsDao;
import lucene.entity.IndexedGoods;
import org.apache.solr.client.solrj.SolrServer;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 */
public abstract class GeneralIndexingServiceImpl extends AbstractIndexingService {
    @Autowired
    private IndexingGoodsDao indexingGoodsDao;

    @Autowired
    @Override
    public void setSolrServer(SolrServer solrServer) {
        super.setSolrServer(solrServer);
    }

    public void setIndexingGoodsDao(IndexingGoodsDao indexingGoodsDao) {
        this.indexingGoodsDao = indexingGoodsDao;
    }

    @Override
    protected Object getIndexedBean(String id) {
        // IndexedGoods g = indexingGoodsDao.find(id); TODO
        IndexedGoods g = indexingGoodsDao.findMultiCatGoods(id);
        return null == g ? null : toIndexedBean(g);
    }

    @Override
    protected long countIndexedBeans() {
        return indexingGoodsDao.count();
    }

    @Override
    protected List<?> getPagedIndexedBeans(int offset, int size) {
        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("offset", offset);
        paramsMap.put("size", size);

        // List<IndexedGoods> many = indexingGoodsDao.findMany(paramsMap); TODO
        List<IndexedGoods> many = indexingGoodsDao.findManyMultiCatGoods(paramsMap);
        return Lists.transform(many, new Function<IndexedGoods, Object>() {
            @Override
            public Object apply(IndexedGoods input) {
                return toIndexedBean(input);
            }
        });
    }

    protected abstract Object toIndexedBean(IndexedGoods entity);
}
